package cn.syncrond.tcm.web.rmi;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.HashMap;
import java.util.Map;

import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.commons.lang3.StringUtils;

import cn.syncrond.tcm.AppContext;

/**
 * @Description: Rmi服务类
 * 	启动Rmi服务
 * @author meepai
 * @date 2016-11-21
 */
public class RmiServer {
	
	/**
	 * 端口
	 */
	private int port = 1009;
	
	/**
	 * 注册地址
	 */
	private String url = "service:jmx:rmi:///jndi/rmi://localhost:1009/jmxrmi";
	
	/**
	 * jmx连接服务器  用于连接 rmi
	 */
	private JMXConnectorServer server;
	
	/**
	 * jmx mbean 服务器    用于注册manage bean
	 */
	private MBeanServer mbeanServer;
	
	public RmiServer(){
		// 获取配置信息中的端口
		String portStr = AppContext.getInstance().getInitRmiServerPort();
		if(StringUtils.isNoneBlank(portStr)){
			try {
				port = Integer.parseInt(portStr.trim());
			} catch(Exception e){}
		}
		// 获取配置信息中的服务地址
		String serviceUrl = AppContext.getInstance().getInitRmiServiceUrl();
		if(StringUtils.isNoneBlank(serviceUrl)){
			url = serviceUrl;
		}
		// 初始化服务器
		init();
	}
	
	/**
	 * 初始化
	 */
	private void init(){
		// 创建一个注册端口
		try {
			LocateRegistry.createRegistry(port);
			// 创建MBeanServer
			mbeanServer = MBeanServerFactory.createMBeanServer();
			// 增加授权
			Map<String, Object> prop = new HashMap<String, Object>();
			// 加入授权   -- 暂时不加
//			prop.put(JMXConnectorServer.AUTHENTICATOR, new JMXAuthenticator() {
//				@Override
//				public Subject authenticate(Object credentials) {
//					if(credentials.equals("u1")){
//						return new Subject();
//					}
//					throw new SecurityException("not authicated");
//				}
//			});
			// 获取绑定地址
			JMXServiceURL jmxServiceURL = new JMXServiceURL(url);
			server = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, prop, mbeanServer);
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 启动服务
	 */
	public void start(){
		try {
			server.start();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 注册管理Bean
	 * @param name
	 * @param object
	 */
	public void registerMBean(String name, Object object){
		try {
			mbeanServer.registerMBean(object, new ObjectName(name));
		} catch (InstanceAlreadyExistsException e) {
		} catch (MBeanRegistrationException e) {
		} catch (NotCompliantMBeanException e) {
		} catch (MalformedObjectNameException e) {
		}
	}
	
	/**
	 * 接触注册
	 * @param name
	 */
	public void unRegisterMBean(String name){
		try {
			mbeanServer.unregisterMBean(new ObjectName(name));
		} catch (MBeanRegistrationException e) {
		} catch (InstanceNotFoundException e) {
		} catch (MalformedObjectNameException e) {
		}
	}
}
